<?php
namespace Common\Model;
use Think\Model;
class AdminModel extends Model{
	protected  $pk = '';
	public function __construct(){
		parent::__construct();
		$this->pk = $this -> getPK();	
	}
	/**
	 * 添加管理员
	 */
	public function addAdmin($name,$password){
		$data = [
	              'username' => $name,
		          'password' => md5($password),
        		];
		$res = $this -> data($data) -> add();
		return $res?$res:false;
	}
	/**
	 * 更新信息 save()方法
	 */
	public function updateAdminSave($id,$data){
		if(!is_int($id) || !is_array($data)){
			return false;
		}
		$where[$this->pk] = $id;
		$res = $this -> where($where) -> save($data);
		return $res?$res:false;
	}
	/**
	 * 更新信息 setField()方法
	 */
	public function updateAdminSetField($id,$key,$value){
	    if(!is_int($id)){
			return false;
		}
		$where[$this->pk] = $id; 
		$res = $this -> $where($where) ->setField($key,$value);
		return $res?$res:false;
	}
    /**
     *  带条件分页管理员列表
     */
	public function getAdminList($where = array('1'=>1),$page="",$limit=""){
		if(!$page || !$limit){
			$res = $this -> where($where) -> select();
		}else{
			$res = $this -> where($where) -> limit($page.','.$limit)->select();
		}
		return $res?$res:false;
	}
	/*
	 * 获取查询条数
	 */
    public function getAdminCount($where){
    	$count = $this -> where($where)->count();
    	return $count?$count:0;
    }
    /**
     * 删除
     */
    public function deleteAdmin($id){
    	if(!is_int($id)){
    		return false;
    	}
    	$where[$this->pk] = $id;
    	$res = $this -> where($where) -> delete();
    	return $res?$res:false;
    }
	
}